//
//  ReciteViewModel.swift
//  XingCeHelper
//
//  Created by 申铭 on 2022/11/3.
//

import Foundation

class ReciteViewModel {
    /// 关键词，唯一标识
    var keyword: String?
    var count: Int = 0
    
    private var index: Int = -1
    private var type: PracticeType!
    private var idioms = [Idiom]()
    private var specifis = [Specification]()
    private var mode: ReciteMode = .weight
    
    init(type: PracticeType?) {
        self.type = type
        
        queryData()
    }
    
    /// 修改背诵模式
    func changeMode(mode: ReciteMode) {
        self.index = -1
        self.mode = mode
        
        if mode == .random { // 随机模式
            idioms.shuffle()
            specifis.shuffle()
        } else { // 权重模式
            queryData() // 获取最新的排序数据
        }
        
        UserDefaults.standard.set("\(mode.rawValue)", forKey: kUserDefaults_ReciteMode)
    }
    
    /// 上一个
    func previous() -> [BaseCellModel]? {
        index = index - 1
        return getData()
    }
    
    /// 下一个
    func next() -> [BaseCellModel]? {
        index = index + 1
        return getData()
    }
    
    // MARK: - private
    
    private func queryData() {
        if type == .reciteIdiom {
            idioms = IdiomStore.query()
            count = idioms.count
        } else if type == .reciteSpecification {
            specifis = SpecificationStore.query(type: .summarize)
            count = specifis.count
        } else if type == .reciteIssuesAndMeasure  {
            specifis = SpecificationStore.query(type: .issuesAndMeasure)
            count = specifis.count
        }
    }
    
    private func getData() -> [BaseCellModel]? {
        if index < 0 {
            index = 0
            return nil
        }
        
        if type == .reciteIdiom {
            if idioms.count == 0 {
                return nil
            }
            
            if index > idioms.count - 1 {
                index = idioms.count - 1
                return nil
            }
            
            return buildIdiomData(idioms[Int(index)])
        } else if type == .reciteSpecification || type == .reciteIssuesAndMeasure {
            if specifis.count == 0 {
                return nil
            }
            
            if index > specifis.count - 1 {
                index = specifis.count - 1
                return nil
            }
            
            return buildSpecifiData(specifis[Int(index)])
        }
        return nil
    }
    
    private func buildSpecifiData(_ specifi: Specification?) -> [BaseCellModel] {
        keyword = specifi?.originalText
        
        var data = [BaseCellModel]()
        
        if specifi?.type ?? 0 == SpecificationType.summarize.rawValue {
            data.append(ItemCellModel(title: "原文：", content: specifi?.originalText))
            data.append(ItemCellModel(title: "规范表达：", content: specifi?.specificationText))
        } else {
            data.append(ItemCellModel(title: "原文：", content: specifi?.originalText))
            data.append(ItemCellModel(title: "问题表达：", content: specifi?.specificationText))
            data.append(ItemCellModel(title: "对策：", content: specifi?.measureText))
        }
        
        return data
    }
    
    private func buildIdiomData(_ idiom: Idiom?) -> [BaseCellModel] {
        keyword = idiom?.title
        
        var data = [BaseCellModel]()
        data.append(TitleCellModel(title: idiom?.title))
        data.append(ItemCellModel(title: "解释：", content: idiom?.explain))
        if let keyPoint = idiom?.keyPoint, !keyPoint.isEmpty {
            data.append(ItemCellModel(title: "重点：", content: idiom?.keyPoint))
        }
        data.append(WebSearchCellModel(title: idiom?.title))
        return data
    }
    
}
